package com.example.a2022application;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner mProSpinner = null;

    @Override
    protected  void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_main);

        // 获取界面布局文件中的 Spinner组件
        mProSpinner = (Spinner) findViewById(R.id.spin_identity);

        String[] arr = {"学生","老师","管理员"};
        // 创建 ArrayAdapter对象
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
        // 为 Spinner设置 Adapter
        mProSpinner.setAdapter(adapter);

        // 为 Spinner设置选中事件监听器
        mProSpinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String content = parent.getItemAtPosition(position).toString();
        switch(parent.getId()){
            case R.id.spin_identity:
                Toast.makeText(SpinnerActivity.this, "身份："+content, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
